import requests
import csv
from parsel import Selector
from 获取新房房源信息页面列表 import main2
import time

COUNT = 1


class GetHouseDetail:
    def __init__(self, url, csv_writer):
        self.url = url
        self.cookies = {
            'isp': 'true',
            'SECKEY_ABVK': 'yN6sMd5ARVvSUB7X+vkXdUXHawnfR0aNG2HSP7QKGdU%3D',
            'BMAP_SECKEY': 'MXqX4OT-tHaH65jB1haMseNV9vdEuzxa_cfS7uPRMIpi8anRFihGLYtxUNr0O94cdiu1IhWaNB5HCDOukMAUIbyu8JlKcHH7G_OOVHv5WN7Uxtk0kXvksjh-ovbPoOQzr38jU1dcSWSWp2MW2bf0g_vFg60fY1_2bHzx-VcbBD_hkDYdlKeGjHV2h14fpTtf',
            'aQQ_ajkguid': '257EB14C-D5FC-4A0C-AE4C-B29EE0023134',
            'sessid': 'ED113258-2FF4-4CD7-B0E7-E168D210D662',
            'ajk-appVersion': '',
            'ctid': '27',
            'id58': 'CrIcp2WZmzZrrx8uRURFAg==',
            'isp': 'true',
            'wmda_uuid': 'de4cc9a7a1748648cbb407042f1df146',
            'wmda_visited_projects': '%3B8788302075828',
            'xxzlcid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl-cid': '5ac0f643503149f1a01135e6b55d55c6',
            'fzq_h': 'cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969',
            'twe': '2',
            'ajk_member_verify': 'nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D',
            'ajk_member_verify2': 'MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D',
            '58tj_uuid': '86ff780a-1d02-4f6c-a3cd-f9f096505666',
            'als': '0',
            '_ga': 'GA1.2.576226070.1704782154',
            '_gid': 'GA1.2.1221734514.1704782154',
            '_ga_DYBJHZFBX2': 'GS1.2.1704782154.1.0.1704782154.0.0.0',
            'ajk_member_id': '251782391',
            'isp': 'true',
            'wmda_session_id_8788302075828': '1704786489109-f2d12be9-6e46-7df3',
            'init_refer': 'https%253A%252F%252Fcs.anjuke.com%252F',
            'new_uv': '2',
            'new_session': '0',
            'obtain_by': '2',
            'ved_loupans': '523197%3A518535%3A518211',
            'lp_lt_ut': 'cf81b7179fa3cc3d93c125035a737bfd',
            'xxzl_cid': '5ac0f643503149f1a01135e6b55d55c6',
            'xxzl_deviceid': 'JYkM5167KiGwHSLS+uMc5C/mliCZz0JN79UAZak3mg6dt0b0ZJGRWukbzdXiBAII',
            'ajkAuthTicket': 'TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704788900976&PBODY=lbEi8qR23bW8mTgu0ohvsQAH7Cg888N2Y6LU-rU7bx00jRYEqLEtrJd3Tl1DVg5QQnQI271TR-lBiFnpQ4NSqS3ofjczZ8ooPbFXbiQPVamQQfW4RF4pOCGsF1vhafPeyaOtsuwMMZWux2gRXKkGbxGGE0bKRDTa9K2sDyTGPfQ&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI',
        }
        self.headers = {
            'authority': 'cs.fang.anjuke.com',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'cache-control': 'no-cache',
            # 'cookie': 'isp=true; SECKEY_ABVK=yN6sMd5ARVvSUB7X+vkXdUXHawnfR0aNG2HSP7QKGdU%3D; BMAP_SECKEY=MXqX4OT-tHaH65jB1haMseNV9vdEuzxa_cfS7uPRMIpi8anRFihGLYtxUNr0O94cdiu1IhWaNB5HCDOukMAUIbyu8JlKcHH7G_OOVHv5WN7Uxtk0kXvksjh-ovbPoOQzr38jU1dcSWSWp2MW2bf0g_vFg60fY1_2bHzx-VcbBD_hkDYdlKeGjHV2h14fpTtf; aQQ_ajkguid=257EB14C-D5FC-4A0C-AE4C-B29EE0023134; sessid=ED113258-2FF4-4CD7-B0E7-E168D210D662; ajk-appVersion=; ctid=27; id58=CrIcp2WZmzZrrx8uRURFAg==; isp=true; wmda_uuid=de4cc9a7a1748648cbb407042f1df146; wmda_visited_projects=%3B8788302075828; xxzlcid=5ac0f643503149f1a01135e6b55d55c6; xxzl-cid=5ac0f643503149f1a01135e6b55d55c6; fzq_h=cd7ce9745e16db1bc94687924442373c_1704737951984_95d6e2b691194383a49fadab5eb18bdf_3740786969; twe=2; ajk_member_verify=nGamCrFRDk0vJp%2FMiRoVQPDhLMdc%2FwFJFQTH9x1D0A8%3D; ajk_member_verify2=MjUxNzgyMzkxfEw3YlJ1Rkd8MQ%3D%3D; 58tj_uuid=86ff780a-1d02-4f6c-a3cd-f9f096505666; als=0; _ga=GA1.2.576226070.1704782154; _gid=GA1.2.1221734514.1704782154; _ga_DYBJHZFBX2=GS1.2.1704782154.1.0.1704782154.0.0.0; ajk_member_id=251782391; isp=true; wmda_session_id_8788302075828=1704786489109-f2d12be9-6e46-7df3; init_refer=https%253A%252F%252Fcs.anjuke.com%252F; new_uv=2; new_session=0; obtain_by=2; ved_loupans=523197%3A518535%3A518211; lp_lt_ut=cf81b7179fa3cc3d93c125035a737bfd; xxzl_cid=5ac0f643503149f1a01135e6b55d55c6; xxzl_deviceid=JYkM5167KiGwHSLS+uMc5C/mliCZz0JN79UAZak3mg6dt0b0ZJGRWukbzdXiBAII; ajkAuthTicket=TT=eadcc4caf4a62d9763ad5f35f6243bbc&TS=1704788900976&PBODY=lbEi8qR23bW8mTgu0ohvsQAH7Cg888N2Y6LU-rU7bx00jRYEqLEtrJd3Tl1DVg5QQnQI271TR-lBiFnpQ4NSqS3ofjczZ8ooPbFXbiQPVamQQfW4RF4pOCGsF1vhafPeyaOtsuwMMZWux2gRXKkGbxGGE0bKRDTa9K2sDyTGPfQ&VER=2&CUID=39e7aRMHy6DDsOmgAVryN-JpTd-Z8guI',
            'pragma': 'no-cache',
            'referer': 'https://cs.fang.anjuke.com/loupan/518211.html',
            'sec-ch-ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
            'sec-ch-ua-mobile': '?0',
            'sec-ch-ua-platform': '"Windows"',
            'sec-fetch-dest': 'document',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-user': '?1',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',
        }
        self.data = {}
        self.csv_writer = csv_writer

    def get_html(self):
        try:
            global COUNT
            time.sleep(1)
            COUNT += 1
            print('COUNT=', COUNT)
            response = requests.get(self.url, headers=self.headers, cookies=self.cookies)
            return response.text
        except Exception as e:
            print(e)

    def parse_html(self, html):
        try:
            selector = Selector(text=html)
            can_left = selector.xpath('//div[@class="can-left"]/div[@class="can-item"]')[:3]
            for can_item in can_left:
                li_tag = can_item.css('div.can-border ul.list li')
                for li in li_tag:

                    name = li.css('div.name::text').get().strip()
                    des = li.css('div.des::text').get().strip()
                    if not des:
                        des_name = li.css('div.des a::text').get()
                        des = li.css('div.des span::text').get()

                        if des is not None and des_name is not None:
                            des = des_name.strip() + des.strip()
                    print(name, des)
                    self.save_data(name, des)
        except Exception as e:
            pass

    def save_data(self, name, des):
        self.data[name] = des

    def run(self):
        html = self.get_html()
        self.parse_html(html)
        print(self.data)
        self.csv_writer.writerow(self.data)




def main():
    # row_url = 'https://cs.fang.anjuke.com/loupan/canshu-518211.html'
    fieldnames = [
        '楼盘名称', '楼盘特点', '参考单价', '楼盘总价', '物业类型', '开发商', '区域位置',
        '楼盘地址', '最低首付', '月供', '楼盘户型', '最新开盘', '交房时间', '售楼处地址',
        '建筑类型', '产权年限', '容积率', '规划户数', '楼层状况', '物业管理费', '物业公司',
        '车位比', '车位数', '绿化率', '工程进度', '售楼处电话', '开间面积', '公共部分精装修',
        '出售类型', '是否分割', '得房率', '写字楼类型', '写字楼级别', '标准层面积', '临近CBD',
        '已签约租户', '招租客群', '办公室面积', '总套数', '周边人群', '临近商圈', '总建筑面积',
        '招商业态', '是否统一管理', '已签约商户', '商业面积'
    ]
    with open('new_house.csv', 'a+', encoding='utf-8-sig', newline='') as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()

    for url in main2():
        f = open('new_house.csv', 'a+', encoding='utf-8-sig', newline='')
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        spider = GetHouseDetail(url, csv_writer)
        spider.run()
        f.close()


if __name__ == '__main__':
    main()

